package com.example.studentmanager.controller;


import com.example.studentmanager.model.Administrator;
import com.example.studentmanager.model.Student;
import com.example.studentmanager.model.Teacher;
import com.example.studentmanager.service.AdministratorService;
import com.example.studentmanager.service.StudentService;
import com.example.studentmanager.service.TeacherService;
import com.example.studentmanager.utils.ValidateImageCodeUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    @Autowired
    AdministratorService administratorService;

    //定义一个专门用来接受生成随机验证码图片请求的方法
    @RequestMapping("getImage")
    public void getImage(HttpServletResponse response, HttpSession session) throws Exception{
        //1.获取一个随机的验证码
        String code = ValidateImageCodeUtils.getSecurityCode();

        //把生成的验证码设置在session域中
        session.setAttribute("code", code);
        //2.根据获取的随机验证码生成一张验证码图片
        BufferedImage image = ValidateImageCodeUtils.createImage(code);
        //3.把生成的验证码图片传输到前端对应发送请求的img标签中
        ImageIO.write(image, "png", response.getOutputStream());

    }





    //定义一个请求用来接受登录操作
    @ResponseBody
    @RequestMapping("login")
    public String login(HttpSession session, @RequestParam("code") String code,@RequestParam("name") String name,@RequestParam("password") String password,@RequestParam("ident") String ident){
        System.out.println("rfregvrfv ws"+ident);
        String code1=(String)session.getAttribute("code");
        session.setAttribute("ident",ident);
        String page="";
        if (code1.equalsIgnoreCase(code)) {
            if ("teacher".equals(ident)) {
                Teacher teacher = teacherService.searchTeacher(Integer.valueOf(name));
                if (teacher != null) {
                    if (teacher.getPassword().equals(password)) ;
                    session.setAttribute("teacher", teacher);
                    page = "teacher";
                } else {
                    page = "error";
                }
            } else if ("student".equals(ident)) {
                Student student = studentService.searchStudentByStunum(Integer.valueOf(name));
                if (student != null) {
                    if (student.getPassword().equals(password)) {
                        session.setAttribute("student", student);
                        System.out.println(student.getStuName());
                        page = "student";
                    } else {
                        page = "error";
                    }
                }
            } else if ("admin".equals(ident)) {
                Administrator administrator = administratorService.searchAdminByUserName(name);
                System.out.println(administrator.getUsername());
                if (administrator != null) {
                    if (administrator.getPassword().equals(password)) {
                        session.setAttribute("admin", administrator);
                        page = "admin";
                    } else {
                        page = "error";
                    }
                }
            }
        }else{
            page="error";
        }
        return  page;
    }


    //用户退出功能
    @RequestMapping("back")
    public String back(HttpSession session){
        //1.清空session域中的数据
        session.invalidate();//清空
        //2.跳转到login.jsp登录页面
        return "redirect:/back/login.jsp";
    }



}
